package com.example.signsystem.service.Impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.signsystem.entity.vo.AttendanceCount;
import com.example.signsystem.entity.po.SignListPO;
import com.example.signsystem.entity.po.StudentsPO;
import com.example.signsystem.mapper.SignListMapper;
import com.example.signsystem.mapper.StudentsMapper;
import com.example.signsystem.service.ISignListService;

import com.qcby.framework.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SignListServiceImpl extends ServiceImpl<SignListMapper, SignListPO> implements ISignListService {


    @Resource
    private StudentsMapper studentsMapper;

    @Override
    public Boolean signIn(Long phone) throws ParseException {
        /**前置校验**/
        preCheck(phone);
        SignListPO signListPO = new SignListPO();
        /**时间转换**/
        String today = DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN);
        StudentsPO studentsPO = studentsMapper.selectOne(new LambdaQueryWrapper<StudentsPO>().eq(StudentsPO::getPhoneNum, phone));
        LambdaQueryWrapper<SignListPO> lam = new LambdaQueryWrapper<SignListPO>().eq(SignListPO::getUserId, studentsPO.getId());
        List<SignListPO> userSign = this.baseMapper.selectList(lam);
        if (CollectionUtil.isNotEmpty(userSign)) {
            /**判断有没有重复签到，当天只能签到一次**/
            List<String> signDay = userSign.stream().map(SignListPO::getSignDay).collect(Collectors.toList());
            if (signDay.contains(today)) {
                throw new ServiceException("500", "当前已经签过到了，请勿重复签到");
            }
        }

        signListPO.setUserId(studentsPO.getId());
        signListPO.setSignDay(today);
        int insert = this.baseMapper.insert(signListPO);
        return insert > 0;
    }

    @Override
    public AttendanceCount todayStudents() {
        /**先查出所有签到的学生**/
        String today = DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN);
        List<SignListPO> signListPOS = this.baseMapper.selectList(new LambdaQueryWrapper<SignListPO>().eq(SignListPO::getSignDay, today));
        List<Long> userIds = signListPOS.stream().map(SignListPO::getUserId).collect(Collectors.toList());
        /**查出所有的学生**/
        List<StudentsPO> studentsPOS = studentsMapper.selectList(null);
        /**非常重要学生列表**/
        List<StudentsPO> studentsVeryImportant=studentsMapper.selectList(new LambdaQueryWrapper<StudentsPO>().eq(StudentsPO::getIsImportant,1));
        /**重要学生列表**/
        List<StudentsPO> studentsImportant=studentsMapper.selectList(new LambdaQueryWrapper<StudentsPO>().eq(StudentsPO::getIsImportant,0));
        List<StudentsPO> noSignStudentsVI = new ArrayList<>();
        List<StudentsPO> noSignStudentsI=new ArrayList<>();
        /**如果说没有签到，就加到列表里面来**/
        for (StudentsPO studentsPO : studentsVeryImportant) {
            if(!userIds.contains(studentsPO.getId())){
                noSignStudentsVI.add(studentsPO);
            }
        }
        for (StudentsPO studentsPO : studentsImportant) {
            if(!userIds.contains(studentsPO.getId())){
                noSignStudentsI.add(studentsPO);
            }
        }
        /**未签到人数**/
        int count = noSignStudentsVI.size()+noSignStudentsI.size();
        /**返回体**/
        AttendanceCount attendanceCount=new AttendanceCount();
        attendanceCount.setNoSignCount(count);
        attendanceCount.setNoSignStudentListV(noSignStudentsVI);
        attendanceCount.setNoSignStudentListI(noSignStudentsI);
        return attendanceCount;
    }

    private void preCheck(Long phone) throws ParseException {
        /**签到时间是否符合**/
        String time = DateUtil.format(new Date(), DatePattern.NORM_TIME_FORMAT);
        String format = "HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date nowTime = sdf.parse(time);
        //范围开始时间
        Date startTimeO = sdf.parse("21:00:00");
        //范围结束时间
        Date endTimeO = sdf.parse("23:00:00");
        if(!DateUtil.isIn(nowTime, startTimeO, endTimeO)){
            throw new ServiceException("500", "未到签到时间");
        }

        /**先查询这个学生是否存在**/
        Long count = studentsMapper.selectCount(new LambdaQueryWrapper<StudentsPO>().eq(StudentsPO::getPhoneNum, phone));
        if (count == 0) {
            throw new ServiceException("500", "该学生不存在，请检查学生姓名");
        }

    }
}
